import React, { Component } from "react";import { connect } from "dva";import { routerRedux } from "dva/router";import { Card, Button, Icon, Input, message, Tag } from "antd";import MarkdownEditor from "@/utils/vditorContent";import { formatMessage } from 'umi-plugin-react/locale';import { agreementAdd, agreementInfo, agreementUpdate, defaultAgreementAdd, defaultAgreementInfo, editUpdateAgreement, getAgreementInfo } from "@/services/platformManagement";

class AgreementInfoEdit extends Component {
	constructor(props) {
		super(props);
		this.state = {
			content: '',
      type:0,//0 用户  1 隐私
      isDefault:false,//是否是超管的默认数据
		};
		this.vEditorRef = React.createRef();
	}

	componentDidMount() {
    const { location:{query,pathname} } = this.props
    const isDefault = pathname.indexOf("sysUserAgreement") != -1 || pathname.indexOf("sysPrivacyAgreement") != -1
    if(isDefault){
      //超管的默认数据
      this.setState({
        type:pathname.indexOf("sysUserAgreement") != -1 ? 0 : 1,
        isDefault
      },() => {
        this.getDefaultData()
      })
    }else{
      this.setState({
        type:query.type
      },() => {
        if(query.id){
          this.getInfo()
        }
      })
    }
	}

  getDefaultData = async () => {
    const { type } = this.state;
    let res = await defaultAgreementInfo({type})
    this.setState({ content: res.content },() => {
      if (this.vEditorRef) {
        this.vEditorRef.current.setContent(res.content);
      }
    })
    
  }

	getInfo = async () => {
    const { location:{query,pathname} } = this.props
		let res = await agreementInfo({id:query.id})
		this.setState({ content: res.content },() => {
      if (this.vEditorRef) {
        this.vEditorRef.current.setContent(res.content);
      }
    })
	}

	onSubmit = async () => {
    const { location:{query} } = this.props
		let { content,isDefault,type } = this.state;
    let res = {}
    if(isDefault){
      //超管数据
      res = await defaultAgreementAdd({content,type})
    }else{
      if(query.editType == 'add'){
        res = await agreementAdd({content,type})
      }
      if(query.editType == 'edit'){
        res = await agreementUpdate({content,type,id:query.id})
      }
    }
    
		if (res.code === 0) {
			message.success(formatMessage({ id: 'platformManagement.agreementInfo.saveSuccess' }));
      if(!isDefault){
        this.toBack()
      }
		}
	}

	setContent = () => {
		let { content } = this.state;
		if (this.vEditorRef) {
            this.vEditorRef.current.setContent(content);
        };
	}

  toBack = () => {
    const { location:{query},history } = this.props;
    history.push({ pathname: query.pathname })
  }

	render() {
		let { content,isDefault,type } = this.state;
    const { location:{query,pathname} } = this.props

		return (
			<Card bordered={false} style={{ height: '100%' }} bodyStyle={{ height: '100%' }}>
				<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 20 }}>
					<span style={{ fontSize: '20px', color: '#000', fontWeight: 'bold' }}>{type == 0 ? formatMessage({ id: 'platformManagement.agreementInfo.userAgreement'  }) : formatMessage({ id: 'platformManagement.agreementInfo.privacyPolicy'  })}</span>
					<div>
            <Button type="primary" onClick={this.onSubmit} style={{marginRight:8}} disabled={query.editType =='view'}>{formatMessage({ id: 'platformManagement.agreementInfo.save'  })}</Button>
            {!isDefault ? <Button type="primary" onClick={this.toBack}>{formatMessage({ id: 'platformManagement.agreementInfo.return'  })}</Button> : ''}
          </div>
				</div>
				<MarkdownEditor
					value={content}
					name='markdown-editor'
					id='markdown-editor'
					height='calc(100% - 52px)'
					width='700'
					onChange={value => this.setState({ content: value })}
					setContent={this.setContent}
					ref={this.vEditorRef}
				/>
			</Card>
		);
	}
}

export default connect(({ user }) => ({
	currentUser: user.currentUser,
}))(AgreementInfoEdit);
